package com.app.service.impl;

import com.app.model.PlatformChangeRecord;
import com.app.mapper.PlatformChangeRecordMapper;
import com.app.service.IPlatformChangeRecordService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 平台用户记录 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-07-18
 */
@Service
public class PlatformChangeRecordServiceImpl extends ServiceImpl<PlatformChangeRecordMapper, PlatformChangeRecord> implements IPlatformChangeRecordService {

    /**
     * 添加平台记录
     *
     * @param pumpingType 泵送类型 1 增加、2减去
     * @param type        类型
     * @param userId      用户id
     * @param typeName    类型名称
     * @param value       值
     * @param beforeValue 值之前
     * @param afterValue  后值
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPlatformRecord(Integer pumpingType,Integer type, Integer userId, String typeName, BigDecimal value, BigDecimal beforeValue, BigDecimal afterValue) {
        PlatformChangeRecord record=new PlatformChangeRecord();
        record.setCreateTime(new Date());
        record.setUserId(Long.valueOf(userId));
        record.setType(type);
        record.setTypeName(typeName);
        record.setAfterValue(afterValue);
        record.setValue(value);
        record.setBeforeValue(beforeValue);
        record.setPumpingType(pumpingType);
        this.save(record);
    }

    /**
     * 统计值
     *
     * @param type      类型
     * @param startTime
     * @param endTime   结束时间
     * @return {@link Object}
     */
    @Override
    public Object statisticalValue(Integer type, String startTime, String endTime) {
        if(type==1)
        {
           BigDecimal statisticalPlatformVoucherValue=this.baseMapper.statisticalPlatformVoucherValue(startTime,endTime);
           BigDecimal statisticalOtherVoucherValue=this.baseMapper.statisticalOtherVoucherValue(startTime,endTime);
           Map<String,Object> map=new HashMap<>();
           map.put("statisticalPlatform",statisticalPlatformVoucherValue);
           map.put("statisticalOther",statisticalOtherVoucherValue);
           return map;
        }else{
            BigDecimal statisticalPlatformVoucherValue=this.baseMapper.statisticalPlatformPointsValue(startTime,endTime);
            BigDecimal statisticalOtherVoucherValue=this.baseMapper.statisticalOtherPointsValue(startTime,endTime);
            Map<String,Object> map=new HashMap<>();
            map.put("statisticalPlatform",statisticalPlatformVoucherValue);
            map.put("statisticalOther",statisticalOtherVoucherValue);
            return map;
        }

    }

    @Override
    public Object platformUserList(Integer page,Integer limit,Integer type, String typeName, String startTime, String endTime) {
        Page<PlatformChangeRecord> p=new Page<>(page,limit);

        IPage<PlatformChangeRecord> list=this.baseMapper.platformUserList(p,type,typeName,startTime,endTime);


        return list;
    }
}
